// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataSourceSet:[],
    itemHeight:'',
    imgList:[]
  },
  getWHBySrc (src)  {
    try {
      const step0 = src.match(/(?<=.*-W)\d+H\d+(?=\/w.*)/gi)[0];
      return {
        w: step0.split("H")[0],
        h: step0.split("H")[1],
      };
    } catch (error) {
      return {};
    }
  },
  getMinTarget  (set){
    let minIndex = 0;
    for (let i = 0; i < set.length; i++) {
      if (set[i].height < set[minIndex].height) {
        minIndex = i;
      }
    }
    return set[minIndex];
  },
  bindscrolltolower(e){
this._onRefresh()
    console.log(e.target)
    if(e.target.offsetTop=42){
      const elequery = wx.createSelectorQuery().in(this);
elequery.selectAll('.goup').boundingClientRect((res)=>{
  console.log(res)
}).exec();
    }
  },
  scrolltoupper(){
    console.log('a')
  },
  useDataSourceSet (dataSource)  {
    
    const {
      dataSourceSet,
      itemHeight
    } = this.data
    for (let i = 0; i < 2; i++) {
      dataSourceSet.push({
        value: [],
        height: 0,
      });
    }
    this.setData({
      dataSourceSet
    })
    dataSource.forEach((imgObj) => {
      const {
        imglist
      } = imgObj || {};
      const {
        w,
        h
      } = this.getWHBySrc(imglist[0]);
      const relH = (itemHeight / w) * h;
      const minTarget = this.getMinTarget(dataSourceSet);
      minTarget.value.push(imgObj);
      minTarget.height = minTarget.height + relH;
    });
    return dataSourceSet;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.getNextData()
  },
  getNextData(){
    const that=this;
    wx.request({
      url: 'http://10.9.41.240:3000/mock/16/banner',
      method:'GET',
      success (res) { 
        that.setData({
          imgList:res.data.list
        })
    
      }
    })
  },
  _onRefresh(){
    const that=this;
      wx.request({
        url: 'http://10.9.41.240:3000/mock/16/banner',
        method:'GET',
        success (res) { 
          that.setData({
            imgList:[...res.data.list,...that.data.imgList]
          })
       
        }
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})